package com.app.newiptv.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.app.newiptv.R;
import com.app.newiptv.bean.UserLogin;
import com.app.newiptv.utils.Paths;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by legend on 19-3-20.
 */

public class LoginActivity extends Activity {
    private AppCompatEditText username;
    private AppCompatEditText password;
    private Button login;
    private SharedPreferences preferences;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (AppCompatEditText)findViewById(R.id.name_edit);
        password = (AppCompatEditText)findViewById(R.id.use_edit);
        login = (Button)findViewById(R.id.sure_button);
        preferences = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String usernameString = username.getText().toString().trim();
                final String passwordString = password.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpUtils.post()
                                .url(Paths.HOSTPATH+Paths.USER_LOGIN)
                                .addParams("u_name", usernameString)
                                .addParams("u_password", passwordString)
                                .build().execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Log.i("legendqi onError", e.toString());
                                Contast.USE_TOKEN = "";
                                Contast.USE_GROUP = "";
                                Contast.USE_UID = "";
                                new AlertDialog.Builder(LoginActivity.this)
                                        .setTitle("警告")
                                        .setMessage("登录失败!")
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        }).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Log.i("legendqi onResponse", response);
                                final UserLogin loginInfo = JSON.parseObject(response,UserLogin.class);
                                if (loginInfo.getCode() == 1){
                                    Contast.USE_TOKEN = loginInfo.getData().getToken();
                                    Contast.USE_GROUP = loginInfo.getData().getU_group();
                                    Contast.USE_UID = loginInfo.getData().getU_id();
                                    SharedPreferences.Editor edit = preferences.edit();
                                    edit.putString("username", usernameString);
                                    edit.putString("password", passwordString);
                                    edit.putString("sousuo", "1");
                                    edit.commit();
                                    Intent intent = new Intent();
                                    intent.putExtra("username", loginInfo.getData().getU_name());
                                    setResult(1001, intent);
                                    onBackPressed();
                                } else {
                                    new AlertDialog.Builder(LoginActivity.this)
                                            .setTitle("警告")
                                            .setMessage("登录失败!")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            }).show();
                                    Contast.USE_TOKEN = "";
                                    Contast.USE_GROUP = "";
                                }
                            }
                        });}}).start();
            }
        });
    }
}
